/*
 * This file is part of PDBEdit.
 * 
 * PDBEdit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * PDBEdit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PDBEdit.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * Creation date : 31/08/2010
 * Copyright (c) Guerfyld 2010. All rights reserved.
 * http://......
 */
package pdbfile;

/**
 * The PDBInteger class manage PilotDB INTEGER field
 *
 * @author guerfyld
 * @version 1.0
 */
public class PDBInteger implements PDBType {
	private long longVal;

	/**
	 * Constructor
	 *
	 * @param val	PilotDB INTEGER value
	 */
	public PDBInteger(long val) {
		longVal = val;
	}

	/**
	 * Constructor
	 *
	 * @param stm	Stream to read
	 * @param pos	Start read position in stream
	 */
	public PDBInteger(PDBInputStream stm, long pos) throws Exception {
		longVal = stm.readValue(pos, 4);
	}

	/**
	 * Set PilotDB INTEGER value
	 *
	 * @param val	PilotDB INTEGER value
	 */
	public void setValue(long val) {
		longVal = val;
	}

	/**
	 * Get PilotDB INTEGER value
	 *
	 * @return	PilotDB INTEGER value
	 */
	public long getValue() {
		return longVal;
	}

	/**
	 * Compares two PDBInteger objects.
	 * The result is a negative integer if this PDBInteger object precedes the argument PDBInteger.
	 * The result is a positive integer if this PDBInteger object follows the argument PDBInteger.
	 * The result is zero if the PDBInteger are equal.
	 *
	 * @param val	The PDBInteger to be compared
	 * @return	Result value
	 */
	public int compareTo(PDBType val) throws IllegalArgumentException {
		if (val instanceof PDBInteger) {
			if(longVal > ((PDBInteger)val).longVal)
				return 1;
			else if(longVal == ((PDBInteger)val).longVal)
				return 0;
			else
				return -1;
		}
		else
			throw new IllegalArgumentException();
	}

	/**
	 * Returns a string representation of the object.
	 *
	 * @return	a string representation of the object.
	 */
	public String toString() {
		return Long.toString(longVal);
	}

	/**
	 * Returns length of raw data
	 */
	public long length() {
		return 4;
	}

	/**
	 * Clone object
	 * @return	New object
	 */
	public PDBType clone() {
		PDBInteger val = new PDBInteger(longVal);
		return val;
	}

	/**
	 * Write fields to stream
	 *
	 * &param stm	Stream to write
	 */
	public void write(PDBOutputStream stm) throws Exception {
		stm.writeValue(longVal, 4);
	}
}